package cn.fql.org.service.impl;

import ch.qos.logback.core.pattern.color.BoldMagentaCompositeConverter;
import cn.fql.basic.service.impl.BaseServiceImpl;
import cn.fql.basic.util.Md5Utils;
import cn.fql.basic.util.StrUtils;
import cn.fql.org.domain.Employee;
import cn.fql.org.mapper.EmployeeMapper;
import cn.fql.org.service.IEmployeeService;
import cn.fql.user.domain.Logininfo;
import cn.fql.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //创建logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//0是管理员
        logininfo.setDisable(true);
        //将logininfo对象添加到数据库并生成自增长id
        logininfoMapper.save(logininfo);//自增长id
        //将logininfo对象设置到employee对象里面去
        employee.setLogininfo_id(logininfo.getId());
        //添加到数据库
        super.save(employee);
        //employeeMapper.save(employee);  这句的代码实质和 super效果是一样的

        //为当前的员工添加角色信息
        if(employee.getRole_id()!=null){
            employeeMapper.addRole(employee);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        /*
        外键：关联字段 + 外键约束
        如果只有关联字段，没有约束可以随便删
        但是如果有关联字段，也有外键约束 - 先删除关联信息，再删除当前表的数据
        开发中：先添加关联字段，不添加外键约束。开发完成之后将约束条件加进去。-方便开发时测试
        */
        //删除角色信息
        employeeMapper.delEmployeeRoleByEmployeeId(id);
        //根据id 查询employee对象
        Employee employee = employeeMapper.findById(id);
        //根据employee对象里面的logininfoid 删除logininfo信息
        logininfoMapper.del(employee.getLogininfo_id());
        //删除employee信息
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        /**
         *修改的时候页面中不会传递 Logininfo_id() 和 shop_id
         * 这两个参数 如果不处理点击修改会变成null
         */
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //查询原来的employee对象
        Employee oldEmployee = employeeMapper.findById(employee.getId());
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        //更新数据t_logininfo
        super.update(employee);
        //从原来的对象里面拿到loginifo 的id
        //更新t_logininfo
        Logininfo oldLogininfo = logininfoMapper.findById(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        //属性同名原则会把员工的id拷贝给登录信息对象的id
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //修改当前员工的信息
        if(employee.getRole_id()!=null){
            //删除角色的信息
            employeeMapper.delEmployeeRoleByEmployeeId(employee.getId());
            //再添加新的角色的信息
            employeeMapper.addRole(employee);
        }
    }

    @Override
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}
